<template>
  <div class="page">
    <!--  头部  -->
    <div class="head">{{title}}</div>
    <!--  中间  -->
    <div class="middle">
      <div class="login-form">
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules">
          <el-form-item prop="username">
            <el-input ref="username" v-model="loginForm.username" type="text" auto-complete="off" placeholder="账号"
              @keyup.enter.native="handleLogin">
              <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
            </el-input>
          </el-form-item>
          <el-form-item prop="phonenumber">
            <el-input ref="phonenumber" v-model="loginForm.phonenumber" type="text" auto-complete="off"
              placeholder="手机电话" @keyup.enter.native="handleLogin">
              <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
            </el-input>

          </el-form-item>
          <el-form-item prop="code">
            <el-input v-model="loginForm.code" auto-complete="off" placeholder="验证码" @keyup.enter.native="handleLogin">
              <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
              <el-button type="primary" slot="append" :disabled="countdown > 0" @click="get_code">
              获取验证码<span v-if="countdown > 0">({{countdown}})</span>
              </el-button>
            </el-input>

          </el-form-item>
          <el-form-item prop="password">
            <el-input v-model="loginForm.password" type="password" auto-complete="off" placeholder="密码"
              @keyup.enter.native="handleLogin">
              <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
            </el-input>
          </el-form-item>
          <el-form-item prop="confirmPassword">
            <el-input v-model="loginForm.confirmPassword" type="password" auto-complete="off" placeholder="确认密码"
              @keyup.enter.native="handleLogin">
              <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
            </el-input>
          </el-form-item>

          <el-form-item style="width:100%;">
            <el-button :loading="loading" type="primary" style="width:100%;" @click.native.prevent="handleLogin">
              <span v-if="!loading">密码重置</span>
              <span v-else>重 置 中...</span>
            </el-button>
            <div style="float: right;">
              <router-link class="link-type" :to="'/login'">使用已有账户登录</router-link>
            </div>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <!--  底部  -->
    <div class="footer">
      <span>Copyright © 2007-2023 华盛天盈科技 All Rights Reserved</span>
    </div>


  </div>
  </div>
</template>

<script>
  import {
    getCodeImg,
    getTitle,
    getCode,
    Update_Pwd
  } from '@/api/login';
  import Cookies from 'js-cookie';
  import {
    encrypt,
    decrypt
  } from '@/utils/jsencrypt';
  export default {
    name: 'Forgot',
    data() {
      const equalToPassword = (rule, value, callback) => {
        if (this.loginForm.password !== value) {
          callback(new Error("两次输入的密码不一致"));
        } else {
          callback();
        }
      };
      return {
        title: process.env.VUE_APP_TITLE,
        codeUrl: '',
        loginForm: {
          username: '',
          phonenumber: '',
          password: "",
          confirmPassword: "",
          code: '',
          uuid: ''
        },
        loginRules: {
          username: [{
            required: true,
            trigger: 'blur',
            message: '请输入您的账号'
          }],
          phonenumber: [{
            required: true,
            trigger: 'blur',
            message: '请输入注册的手机号'
          }],
          password: [{
              required: true,
              trigger: 'blur',
              message: '请输入您的密码'
            },
            {
              min: 6,
              max: 20,
              message: '用户密码长度必须介于 6 和 20 之间',
              trigger: 'blur'
            }
          ],
          confirmPassword: [{
              required: true,
              trigger: "blur",
              message: "请再次输入您的密码"
            },
            {
              required: true,
              validator: equalToPassword,
              trigger: "blur"
            }
          ],
          code: [{
            required: true,
            trigger: 'blur',
            message: '请输入验证码'
          }]
        },
        loading: false,
        // 验证码开关
        captchaEnabled: true,
        // 注册开关
        register: false,
        redirect: undefined,
        countdown: 0 // 设定初始倒计时时间
      };
    },
    watch: {

    },
    created() {
      this.get_itle();
      this.$nextTick(() => {
        this.$refs.username.$refs.input.focus();
      });

    },
    methods: {
      startTimer() {
        const interval = setInterval(() => {
          if (this.countdown > 0) {
            this.countdown--
          } else {
            clearInterval(interval)
          }
        }, 1000)
      },
      get_code() {
        if (!this.loginForm.username) {
          this.$message.error("账号不能为空");
          return;
        }
        if (!this.loginForm.phonenumber) {
          this.$message.error("手机号不能为空");
          return;
        }
        var Params = {
          username: this.loginForm.username,
          phonenumber: this.loginForm.phonenumber
        }
        this.$modal.loading("正在获取验证码...");
        getCode(Params).then(response => {
          this.countdown=120;
          this.startTimer();
          this.$message.success("验证码已发送手机，2分钟内有效");
          this.$modal.closeLoading();
        }).catch(() => {
          this.$modal.closeLoading();
        });;
      },
      onkeyup(e) {

      },
      get_itle() {
        var Params = {}
        getTitle(Params).then(response => {
          this.title = response.data ? response.data : this.title;
        })

      },

      handleLogin() {
        this.$refs.loginForm.validate(valid => {
          if (valid) {
            this.loading = true;
            var Params = {
              username: this.loginForm.username,
              phonenumber: this.loginForm.phonenumber,
              password: this.loginForm.password,
              confirmPassword: this.loginForm.confirmPassword,
              code: this.loginForm.code
            }
            this.$modal.loading("保存中...");
            Update_Pwd(Params).then(response => {
              this.$message.success("密码修改成功");
              this.loading = false;
              this.$modal.closeLoading();
              this.$router.push({
                path: `/login`,
              });
            }).catch(() => {
              this.loading = false;
              this.$modal.closeLoading();
            });;
          }
        });
      }
    }
  };
</script>

<style rel="stylesheet/scss" lang="scss">
  .page {
    height: 100%;
    width: 100%;
    display: flex;
    flex-direction: column;
  }

  .head {
    height: 96px;
    display: flex;
    align-items: flex-end;
    padding-left: 58px;
    margin-bottom: 20px;
    font-weight: bold;
    font-size: 36px;
    color: #3D7DE0;
    letter-spacing: 3px;
  }

  .middle {
    flex: 1;
    background: url(../assets/images/m_bg.png) no-repeat center;
    background-color: #3D7DE0;
    display: flex;
    align-items: center;

  }

  .footer {
    height: 96px;
    line-height: 96px;
    width: 100%;
    text-align: center;
    font-family: Arial;
    font-size: 14px;
    letter-spacing: 1px;
  }

  .login-form {
    border-radius: 6px;
    position: absolute;
    right: 10%;
    // margin-top: 20px;
    background: #ffffff;
    width: 400px;
    padding: 25px 25px 5px 25px;

    .login-title {
      color: #3D7DE0;
      font-weight: 500;
      font-size: 24px;
      text-align: center;
      letter-spacing: 20px;
      margin-bottom: 20px;
    }

    .el-input {
      height: 38px;

      input {
        height: 38px;
        // padding-bottom: 10px;
        text-indent: 5px;
        background: transparent;
        border: none;
        border-radius: 0;
        color: #333;
        border-bottom: 1px solid rgb(235, 237, 242);
      }
    }

    .input-icon {
      height: 39px;
      width: 14px;
      margin-left: 2px;
    }

    .el-button--small {
      padding: 16px;
      font-size: 16px;
    }
  }

  .login-tip {
    font-size: 13px;
    text-align: center;
    color: #bfbfbf;
  }

  // .login-code {
  //   width: 33%;
  //   height: 38px;
  //   float: right;
  //   img {
  //     cursor: pointer;
  //     vertical-align: middle;
  //   }
  // }
  .login-code {
    display: flex;
    align-items: center;
    justify-content: space-around;
    margin: 0 0 0 10px;
    width: 33%;
    height: 38px;
    float: right;
    cursor: pointer;

    .login-code-img {
      margin-top: 2px;
      width: 100px;
      height: 38px;
      background-color: #fdfdfd;
      border: 1px solid #f0f0f0;
      color: #333;
      font-size: 18px;
      font-weight: bold;
      letter-spacing: 5px;
      line-height: 38px;
      text-indent: 5px;
      text-align: center;
    }
  }

  .el-input-group__append {
    border: 0;
    border-radius: 0;
  }
</style>
